// pages/views/login/login.js
const utils = require('../../../utils/util.js');
const root = getApp().globalData.rooturl;
const RSA = require('../../../utils/rsa.js');
const key = require('../../../utils/key.js').key;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        phone: "",
        phonecode: "",
        codewait: '',
        getcodestate: '0',
        lbdis: false,
        cbdis: false,
        isloading: false,
        btnstate: '获取验证码',
        userpic:'',
        usernickname:'',
        wxid:''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userpic:options.pic,
            wxid:options.openid,
            usernickname:options.nickname
        })
    },
    //输入监听
    phoneInp: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    codeInp: function (e) {
        this.setData({
            phonecode: e.detail.value
        })
    },
    //获取验证码
    getCode: function () {
        if (utils.empty(this.data.phone) == false) {
            utils.errorToast('手机号不能为空');
            return false;
        }
        if (utils.isPhone(this.data.phone) == false) {
            utils.errorToast('请输入正确格式手机号');
            return false;
        }
        let _this = this;
        //获取验证码请求
        utils.ajax(root + 'wechatusers/SendPhoneVcode', {
            header: {
                "SOURCE": 'wechatApp',
                "Content-Type": 'application/x-www-form-urlencoded'
            },
            data: {
                phone: this.data.phone,
                type: 'login',
                restType: 0
            },
            method: 'post'
        }, function (res) {
            if (res.code == "1" || res.code == '2001') {
                wx.showToast({
                    title: '验证码发送成功',
                    icon: 'none'
                })
                _this.setData({
                    hasloading: false
                })
                utils.setCodeBtn('codewait2', 60, _this);
            } else {
                wx.showToast({
                    title: '验证码发送失败,请稍后再试',
                    icon: 'none'
                })
                _this.setData({
                    cbdis: false,
                    hasloading: false
                })
            }
        }, function (res) {
            wx.showToast({
                title: '网络错误，请求失败',
                icon: 'none'
            })
            _this.setData({
                cbdis: false,
                hasloading: false
            })
        })
    },
    //登录
    pushLogin: function (e) {
        if (utils.empty(this.data.phone) == false) {
            utils.errorToast('手机号不能为空');
            return false;
        }
        if (utils.isPhone(this.data.phone) == false) {
            utils.errorToast('请输入正确格式手机号');
            return false;
        }
        if (utils.empty(this.data.phonecode) == false) {
            utils.errorToast('验证码不能为空');
            return false;
        }
        this.setData({
            lbdis: true,
            isloading: true
        })
        this.sendLogin(this.data.phone, this.data.phonecode)
    },
    //发送登录请求
    sendLogin: function (phone, phonecode) {
        let _this = this;
        let data = this.encrypt(phone, phonecode);
        let ldata= {
            restType: 0,
            data: data,
            wx_headimgurl: _this.data.userpic,
            wx_nickname: _this.data.usernickname,
            wx_id:_this.data.wxid,
            wx_unionid:''
        };
        console.log(ldata)
        utils.ajax(root + "wechatusers/WxBindPhone", {
            method: 'post',
            header: {
                "SOURCE": 'wechatApp',
                "Content-Type": 'application/x-www-form-urlencoded'
            },
            data: {
                restType: 0,
                data: data,
                wx_headimgurl: _this.data.userpic,
                wx_nickname: _this.data.usernickname,
                wx_id:_this.data.wxid,
                wx_unionid:''
            },
        }, function (res) {
            if (res.code == '1') {
                wx.showToast({
                    title: '登录成功',
                    icon: 'none'
                })
                res.data.timeout = new Date().getTime();
                wx.setStorageSync('user_info', res.data);
                //发送用户信息
                utils.sendUserMsg(res.data);
                _this.setData({
                    lbdis: false,
                    isloading: false,
                })
                wx.switchTab({
                    url: '../../index/index'
                })
            } else {
                _this.setData({
                    lbdis: false,
                    isloading: false,
                })
                wx.showToast({
                    title: '登录失败，请重试',
                    icon: 'none'
                })
            }
        }, function (err) {
            _this.setData({
                lbdis: false,
                isloading: false,
            })
            wx.showToast({
                title: '登录失败，网络错误',
                icon: 'none'
            })
        })
    },
    //参数加密
    encrypt: function (phone, phonecode) {
        let rsa_type = new RSA.RSAKey();
        rsa_type = RSA.KEYUTIL.getKey(key);
        let arg = {
            phone: phone,
            code: phonecode
        };
        let encStr = rsa_type.encrypt(JSON.stringify(arg))
        encStr = RSA.hex2b64(encStr);
        return encStr;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})